package com.miukoo.aikao.qmanage.controller.v1;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.miukoo.aikao.core.dtos.*;
import com.miukoo.aikao.core.utils.ApiUtils;
import com.miukoo.aikao.core.utils.BeanMapperUtils;
import com.miukoo.aikao.qmanage.dto.RepositoryDto;
import com.miukoo.aikao.qmanage.dto.request.RepositoryQuestionBatchReqDto;
import com.miukoo.aikao.qmanage.dto.response.RepositoryResDto;
import com.miukoo.aikao.qmanage.pojos.Repository;
import com.miukoo.aikao.qmanage.service.RepositoryQuestionService;
import com.miukoo.aikao.qmanage.service.RepositoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
* <p>
* 题库控制器
* </p>
*
* @author 聪明笨狗
* @since 2020-05-25 13:25
*/
@Api(tags={"题库"})
@RestController
@RequestMapping("/qmanage/repository/v1")
public class RepositoryController {

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RepositoryQuestionService repositoryQuestionService;

    /**
    * 添加或修改
    * @param reqDto
    * @return
    */
    @ApiOperation(value = "添加或修改")
    @RequestMapping(value = "/save", method = { RequestMethod.POST})
    public ApiResult save(@RequestBody RepositoryDto reqDto) {
        repositoryService.save(reqDto);
        return ApiUtils.success();
    }

    /**
    * 批量删除
    * @param reqDto
    * @return
    */
    @ApiOperation(value = "批量删除")
    @RequestMapping(value = "/delete", method = { RequestMethod.POST})
    public ApiResult edit(@RequestBody BaseIdsReqDto reqDto) {
        //根据ID删除
        repositoryService.removeByIds(reqDto.getIds());
        return ApiUtils.success();
    }

    /**
    * 查找详情
    * @param reqDto
    * @return
    */
    @ApiOperation(value = "查找详情")
    @RequestMapping(value = "/detail", method = { RequestMethod.POST})
    public ApiResult<RepositoryDto> find(@RequestBody BaseIdReqDto reqDto) {
        Repository entity = repositoryService.getById(reqDto.getId());
        RepositoryDto dto = new RepositoryDto();
        BeanUtils.copyProperties(entity, dto);
        return ApiUtils.success(dto);
    }

    /**
    * 分页查找
    * @param reqDto
    * @return
    */
    @ApiOperation(value = "分页查找")
    @RequestMapping(value = "/paging", method = { RequestMethod.POST})
    public ApiResult<PageResDto<List<RepositoryResDto>>> paging(@RequestBody PageReqDto<RepositoryDto> reqDto) {
        //分页查询并转换
        PageResDto<List<RepositoryResDto>> page = repositoryService.paging(reqDto);
        return ApiUtils.success(page);
    }

    /**
     * 批量操作
     * @param reqDto
     * @return
     */
    @ApiOperation(value = "批量操作", notes = "批量加入或从题库移除")
    @RequestMapping(value = "/batch-action", method = { RequestMethod.POST})
    public ApiResult paging(@RequestBody RepositoryQuestionBatchReqDto reqDto) {
        //分页查询并转换
        repositoryQuestionService.batchAction(reqDto);
        return ApiUtils.success();
    }

    /**
     * 查找列表，每次最多返回200条数据
     * @param reqDto
     * @return
     */
    @ApiOperation(value = "查找列表")
    @RequestMapping(value = "/list", method = { RequestMethod.POST})
    public ApiResult<List<RepositoryDto>> list(@RequestBody RepositoryDto reqDto) {
        //分页查询并转换
        QueryWrapper<Repository> wrapper = new QueryWrapper<>();
        //转换并返回
        List<Repository> list = repositoryService.list(wrapper);
        //转换数据
        List<RepositoryDto> dtoList = BeanMapperUtils.mapList(list, RepositoryDto.class);
        return ApiUtils.success(dtoList);
    }
}
